package com.etoak.common.web.handler;

import com.etoak.common.core.vo.ResultVO;
import com.etoak.common.web.exception.CustomException;
import jakarta.validation.ConstraintViolation;
import jakarta.validation.ConstraintViolationException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;


import java.util.stream.Collectors;

/**
 * Spring MVC 全局异常处理器
 */
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {
    @ExceptionHandler
    public ResultVO<Object> handler(MethodArgumentNotValidException e) {
        log.error(e.getMessage(), e);
        String message = e.getAllErrors().stream().map(ObjectError::getDefaultMessage).collect(Collectors.joining(";"));
        return ResultVO.failed(message);
    }

    @ExceptionHandler
    public ResultVO<Object> handler(ConstraintViolationException e) {
        log.error(e.getMessage(), e);
        String message = e.getConstraintViolations().stream().map(ConstraintViolation::getMessage).collect(Collectors.joining(";"));
        return ResultVO.failed(message);
    }

    @ExceptionHandler
    public ResultVO<Object> handler(CustomException e) {
        log.error(e.getMessage(), e);
        return ResultVO.failed(e.getMessage());
    }

    /**
     * 处理其他位置异常 返回系统异常
      * "code":500
     *  "message":"系统异常!"
     * @return
     */
    @ExceptionHandler
    public ResultVO<Object> handler(Exception e) {
        log.error(e.getMessage(), e);
        return ResultVO.failed();
    }
}
